/**************************************************************************
 *                                                                        *
 *         Copyright (c) 2007 by Sunplus mMedia Inc.                      *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus        *
 *  mMedia Inc. All rights are reserved by Sunplus mMedia Inc.  This      *
 *  software may only be used in accordance with the corresponding        *
 *  license agreement. Any unauthorized use, duplication, distribution,   *
 *  or disclosure of this software is expressly forbidden.                *
 *                                                                        *
 *  This Copyright notice MUST not be removed or modified without prior   *
 *  written consent of Sunplus mMedia Inc.                                *
 *                                                                        *
 *  Sunplus mMedia Inc. reserves the right to modify this software        *
 *  without notice.                                                       *
 *                                                                        *
 *  Sunplus mMedia Inc.                                                   *
 *  19-1, Innovation First Road, Science-Based Industrial Park,           *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 *  Author: sunyong                                                *
 *                                                                        *
 **************************************************************************/
#ifndef __Video_H__
#define __Video_H__

#include "solution.h"//eric 

/**
 * @filename 	video.h
 * @brief 	The header file of video flow
 * @author 	sunyong
 * @since 	2007-12-1
 * @date 	2007-12-1
 */

/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
#define AVI_PCM			1			
#define VLC_BUF_CHG_EN	1

#define video_zoomMax      400
#define video_zoomMin      100
#define video_zoomMaxLog   100
#define video_zoomMinLog   25
#define video_zoomStep     10
#define Video_ZOOM_LogStep 3 // hlc 0328 improve zoom quality

#define	VID_REC_IDLE	 		0
#define	VID_REC_RUN	 		1
#define	VID_REC_PAUSE    		2

#define	VID_PB_IDLE	 		0
#define	VID_PB_RUN	 			1
#define	VID_PB_PAUSE    		2
#define	VID_PB_FW    			3
#define	VID_PB_RW    			4

#define vid_pb_speed_normal 			1
#define vid_pb_speed_FW2 			2
#define vid_pb_speed_FW4 			4
#define vid_pb_speed_FW8 			8

#define vid_pb_speed_RW2 			2
#define vid_pb_speed_RW4 			4
#define vid_pb_speed_RW8 			8

#define  vid_VGA							((UINT16)640)
#define  vid_QVGA						((UINT16)320)
#define  vid_D1             ((UINT16)720)
#define  vid_HD             ((UINT16)1280)

#define	VID_MODE		0 
//#define	AUD_MODE 		1

#define full 							1
#define empty 						0
#define SECPERCHK	((UINT32)11)
#define CHUNKSIZE ( (UINT32)0x01<<SECPERCHK) 
#define VID_TOTAL_FRAGSIZE			((UINT32)4*(UINT32)1024 * (UINT32)64)
#define fragBufSize 					((UINT32)1024 * (UINT32)64)
#define FRAGBUFCNT					(VID_TOTAL_FRAGSIZE /fragBufSize)
#define secPerFragBuf  				(fragBufSize/((UINT32)CHUNKSIZE))

#define VID_PCMFMT 			((UINT16)0x0001)
#define VID_ADPCMFMT 		((UINT16)0x0011)

#define blockAlign 	((UINT32)512)//256
//#define sampleRate	((UINT32)44100)// 22050
#define sampleBit	16
#define channel		1
#define recordMode  	0
#define SMPPERBLK ((blockAlign - 4 ) * 2 + 1)

//#define AudDMACount 	(UINT8)(K_SDRAM_AviCapAudioBufSize / REC_CHUNK_SIZE)
#define AUDBUFSIZE ((UINT32)64 *(UINT32)1024)//K_SDRAM_AviCapAudioBufSize
#define RecordVidMaxCnt  			(UINT16)100//000
#define fragSize_idx	((UINT32)64*1024)

#define VidPb_OpenTwoFile

#define VIDEO_PB_BRC_EN
#define VIDEO_REC_BRC_EN

/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
typedef struct appVidClipCtrl_s {
			UINT8 mode;
			UINT8 recIdle;
			UINT16 fHandle;
			UINT16 fHandle_idx;
			UINT16 fHandle_hdr;
			UINT8 name[12];
			UINT8 name_idx[12];
			UINT8 name_hdr[12];
			UINT8 Qtable;
			UINT32 time;
			UINT16 imgWidth;
			UINT16 imgHight;
			UINT32 Compsize;		
			UINT32 SecAddr;
			UINT32 idxSize;
			UINT32 hdrSize;
			UINT32 SecCnt;
			/*
			UINT32 AVLCAddr;
			UINT32 BVLCAddr;
			UINT32 CVLCAddr;
			UINT32 DVLCAddr;

			UINT32 EVLCAddr;
			UINT32 FVLCAddr;
			UINT32 GVLCAddr;
			UINT32 HVLCAddr;
			UINT32 IVLCAddr;
			UINT32 JVLCAddr;
			*/
			UINT32 DRAMAudBufCnt;
			UINT32 MoviOfset;
			UINT32 VidChunkCnt;
			UINT32 AudChunkCnt;
#ifdef DOS32_SUPPORT
			UINT32 freeCluster;
			UINT32 usedCluster;
#else
			UINT16 freeCluster;
			UINT16 usedCluster;
#endif
			UINT32 DramDMAaddr;
			UINT32 CurDMATagAddr;
			UINT8 CurDMATagStatus;
			UINT32 CurSrcImgAddr;
			UINT8 CurSrcImgStatus;
			UINT32 totalChuck;  
			UINT32 usedSize;
			UINT32 AudioAddr;
			UINT32 AVIHeadAddr;
			UINT32 AVIIdxAddr;
			UINT8 audFmt;
			UINT16 sampRate;
			UINT8 frameRate;
}appVidClipCtrl_t;

extern struct appVidClipCtrl_s vidClipCtrl;

typedef struct appVidPbCtrl_s{
	UINT8 	Idle;
	UINT16 	fHandle;
	UINT8 	name[12];
	UINT8 	speed;
	UINT32    chunckCnt;
	UINT32    vidFrameTime;
	UINT32	playAudTime;
	UINT8 	feedData;
	UINT8	mediaType;
	UINT8	audDown;
	UINT8	vidDown;
	UINT8	showBG;
	
	UINT32 	audInIdx;
	UINT32	lastAudCluster;
	UINT32	lastAudFileIdx;
	UINT32	audAddr;
	UINT32 	pbAudioChunkAddr; 
	UINT32	nextAudIdx;
	UINT32	lastAudIdx;
	UINT8	playAudFlg;

	UINT32	AVIIdxAddr;
	UINT32	lastVidFileIdx;
	UINT32	lastVidCluster;
	UINT8	VLCFlip;
	UINT8	dropVidFlg;
	UINT32	firstVidBlock;
	UINT32	TimeExceed;
	UINT32	AVIIdxRamAddr;
	UINT32	lastVidIdx;
	UINT32	NextVidIdx;
	UINT32 	playVidTime; 
	UINT32	 msPerFrame;
	UINT32 	vidChunkCnt;
	UINT32	audChunkCnt;
	UINT32 	audChunk;
	UINT32 	vidChunk;
	UINT32   audFmt;
	UINT32   audChnnel;
	UINT32   audFrq;
	UINT32   audAlign;
	UINT32   audBit;
	UINT8	dummyFrame;
	UINT32 frameRate;

#ifdef VidPb_OpenTwoFile
    UINT16  fHandle_idx;
    UINT32  fstIdxInBuf;
    UINT32  lstIdxInBuf;
    UINT32  idxReadOff;
#endif
}appVidPbCtrl_t;

extern struct appVidPbCtrl_s vidPbCtrl;

typedef enum VDStampMode_e{
    VD_STAMP_MODE_DATE = 0,
    VD_STAMP_MODE_IMG,
    VD_STAMP_MODE_IMG_DATE,
    
    VD_STAMP_MODE_END,
}VDStampMode_t;

typedef enum VDStampRecMode_e{
    VD_STAMP_REC_MODE_ALL_FRAME,
    VD_STAMP_REC_MODE_FST_FRAME,
    VD_STAMP_REC_MODE_END_FRAME,    
    VD_STAMP_REC_MODE_FST_END_FRAME,
    
    VD_STAMP_REC_MODE_END,
}VDStampRecMode_t;

typedef enum VDParam_e {
    VD_STAMP_ALIG_MODE,
    VD_STAMP_DATATIME_FMT,    
    VD_STAMP_DATETIME_DIS_X_RATIO,
    VD_FRONT_FPS,
    VD_DUMY_MODE,
    VD_PARAM_MAX,
} VDParam_t;

typedef enum ResAviHeader_e {
	RES_AVIHeader = 0x01,
	RES_AVIHeader_PCM = 0x03,
	RES_VideoHeader = 0x0b,
} ResAviHeader_t;
   
#define VD_STAMP_LEFT           0x00
#define VD_STAMP_CENTER         0x01
#define VD_STAMP_RIGHT          0x02
#define VD_STAMP_TOP            0x00
#define VD_STAMP_VCENTER        0x10
#define VD_STAMP_BOTTOM         0x20

#define VD_STAMP_FMT_YYYYMMDD 0x00
#define VD_STAMP_FMT_DDMMYYYY 0x01
#define VD_STAMP_FMT_MMDDYYYY 0x02
#define VD_STAMP_FMT_HHMM     0x10
#define VD_STAMP_FMT_HHMMSS   0x30
#define VD_STAMP_FMT_STR_WITH_DATE            0x40
#define VD_STAMP_FMT_STR_ONLY                     0x80 

#define VID_STAMP_FNT_NUM 21    //date num=10(YYYY/MM/DD) time num=8(hh:mm:ss)
#define VID_STAMP_FNT_MAX_H_SIZE    DATE_STAMP_H_SIZE_3 //should multi of 8
#define VID_STAMP_FNT_MAX_V_SIZE    DATE_STAMP_V_SIZE_3
#define VID_STAMP_MAX_WIDTH 720 //K_VIDEOCLIP_Width
#define VID_STAMP_MAX_HIGH  VID_STAMP_FNT_MAX_V_SIZE
//#define K_SDRAM_AviStampBufSize ((UINT32)VID_STAMP_FNT_MAX_H_SIZE*VID_STAMP_FNT_MAX_V_SIZE*VID_STAMP_FNT_NUM)
#define K_SDRAM_AviStampBufSize ((UINT32)VID_STAMP_MAX_WIDTH*VID_STAMP_MAX_HIGH)
#define K_SDRAM_AviStampFontBufSize ((UINT32)VID_STAMP_FNT_MAX_H_SIZE*VID_STAMP_FNT_MAX_V_SIZE*DATE_STAMP_NUM)
#define K_SDRAM_AviStampBufTotalSize ((UINT32)K_SDRAM_AviStampBufSize*3+K_SDRAM_AviStampFontBufSize)
typedef struct {
    UINT16 imgW;
    UINT16 imgH;

    UINT16 ftW;
    UINT16 ftH;
    UINT32 ftAddr;

    UINT16 dateW;
    UINT16 dateHOff;    //related to stamp image
    UINT16 dateVOff;
    
    UINT16 stampW;
    UINT16 stampH;
    UINT32 stampAddr;   //A, B buffer
    UINT32 imgAddr;     //img or background img addr
    
    UINT16 disHOff;     //related to img
    UINT16 disVOff;     //
    UINT8  disBufId;    //0: A buffer, 1: B buffer        

    //UINT8  disTime; //just save second
}videoStampCfg_t;

#define VD_DUMY_MODE_SIZE0 0
#define VD_DUMY_MODE_COPY_IDX   1
#define VD_DUMY_MODE_COPY_FRAME 2

/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/

/**************************************************************************
 *               F U N C T I O N    D E C L A R A T I O N S               *
 **************************************************************************/
 UINT8* vidLibVerGet(void);
 //video_clip.c
 void videoRecEofIntrEnable(void);
 void videoRecEofIntrDisable(void);
void videoRecSizeSet (UINT16 size);
void videoRecSizeGet (UINT16* width, UINT16* height) ;
void videoRecAudFmtSet(UINT8 fmt);
 void videoRecQTableSet(UINT8 value);
void videoRecStart(void) ;
void videoRecStop (void);
UINT8 videoRecInit(void);
void videoRecHdrInit(void);
UINT8 videoRec(void);
void videoRecPause (void);
void videoRecResume (void) ;
UINT8 videoRecAbort(void);
void videoRecGlobalInit(void);
void videoRecWriteAudID2Dram(UINT32 addr);
void videoRecWriteVidIndex2Dram(UINT32 MoviOfset, UINT32 size , UINT32 AVIIdxAddr);
void videoRecWriteAudIndex2Dram(UINT32 MoviOfset, UINT32 AVIIdxAddr) ;
void videoFillDramLong(UINT32 address, UINT32 value);
UINT8 videoRecStatusGet(void);
void videoRecZoomFacSet(UINT16 zfac, UINT8 videozoom)  ;
UINT8  videoRecWriteIndexToCard(UINT8 integer,UINT32 size);
void videoRecAddJunk(UINT32 addr, UINT32 size);
void videoRecWriteMovIDToDram(UINT32 movAddr);

UINT16 videoZoomFctGet(void) ;
UINT32 videoPvFreeAddrGet(void);
void videoDateStampInit(void);
void videoDateStamp(void);
void videoRecGTSet(UINT32 pausetime);
UINT32 videoRecGTGet(void);
UINT32 videoRecTimeGet(void);
void videoRecFrameRateSet (UINT8 rate) ;
void videoRecAudDataProcess(UINT8 );
void videoRecAddDummy(UINT32 addr);
void videoRecEofIntrEnable(void);
void videoRecEofIntrDisable(void);
void INTR_videoRecVlcBufSet(void);
UINT8 INTR_videoRecReadVlcSize(void);
void videoRecVlcDataProcess(void);
UINT32 videoRecAVLCAddrGet(void);
UINT32 videoRecBVLCAddrGet(void);
void INTR_videoRecVlcBufSwitch(void);
void videoRecAudSampleRateSet(UINT16 rt);
void videoRecAudSampleRateGet(UINT16* rt);

void videoRecAudioStsSet(UINT8 en);
UINT8 videoRecAudioStsGet(void);

void videoRecStampCfg(UINT8 mode, UINT8 aligFmt, UINT8 recMode);
UINT8 videoRecStampImgResLoad(UINT16 resFileId, UINT16 stampW, UINT16 stampH);
void videoRecStampStsSet (UINT8 en);
UINT8 videoRecStampStsGet(void);
void videoRecStampDateFmtSet(UINT8 fmt);
UINT8 videoRecStampDateFmtGet(void);

UINT8 videoRecParamSet(UINT8 paraId, UINT16 value);
UINT16 videoRecParamGet(UINT8 paraId);

//video_play.h
UINT8 videoPbInit(UINT16 ID);
void videoPb(void) ;
void videoPbAbort(void) ;
void videoPbStart(UINT16 ID);
void videoPbStop(void);
void videoPbPause(void);
void videoPbResume(void);
 void videoPbRelease(void);
void videoPbFWSpeedSet(UINT8 speed);
void videoPbRWSpeedSet(UINT8 speed);
 void videoPbFW(void);
 void videoPbFR(void);
 UINT32 videoPbGetAudIdx(UINT32 );
UINT8 videoPbAudioChunck(UINT32 AVIIdxAddr, UINT32 audioBufferAddr, UINT16 fHandle, UINT32 AudioIdx,UINT8  flg) ;
UINT32 videoPbSearchAVIIndex(UINT32 AVIIdxRamAddr, UINT32 defaultIdx, UINT32 maxIdx,UINT32 indexcount, UINT8 type);
UINT32 videoPbSearchAVIIndexBackward(UINT32 AVIIdxRamAddr, UINT32 defaultIdx, UINT32 maxIdx,UINT32 indexcount,  UINT8 type ) ;
UINT8 videoPbReadAVIIdx(UINT32  AVIIdxRamAddr, UINT32 AVIIdxAddr, UINT16 fHandle,UINT32* ChunkCnt, UINT8 onlyFirst);
UINT8  videoPbOneFrame(UINT32 AVIIdxAddr, UINT16 fHandle,UINT32 PBVideoAddr, UINT16 AVIWidth,UINT16 AVIHeight,UINT32 frameNo,UINT8 VLCFlip)	;
UINT8 videoPbFrame(UINT32 AVIIdxAddr, UINT16 fHandle, UINT32 frameNo,UINT8 VLCFlip);
void videoPbGlobalInit(void) ;
UINT32 videoPbReadDword(UINT32 dramAddr) ;
UINT32 videoPbReadSramLong(UINT32 addr);
UINT8 AVIHeaderParse(void);
void videoPbMsGet(UINT32 *pTime);
void SetPbBufAddr(UINT32 addr);
void SetPbBufHsize(UINT16 width);
UINT8 videoPbStatusGet(void) ;
UINT8 videoPbSpeedGet(void);
void videoPbSizeGet(UINT16 *phSize, UINT16 *pvSize);
UINT8 videoPbFrameRead(UINT32 AVIIdxAddr, UINT16 fHandle, UINT32 frameNo, UINT8 VLCFlip);
void videoPbVLCDecode(void);
void videoPbGTSet(UINT32 pausetime);
UINT32 videoPbGTGet(void);
UINT32 videoPbTimeGet(void);
void videoPbStreamRead(UINT16 fHandle, UINT32 dataOffSet);
UINT8 videoPbVLCRingBufCntGet(UINT32 *cnt);
void videoPbAudioPlay(void);
UINT32 videoPbAviLengthGet(void);
void videoPbSlowFWSpeedSet(UINT8 speed);
UINT8 videoRecStampEofGet(void);
void videoRecStampEofSet(UINT8 eofFlag);
void INTR_videoRecStampEofSet(UINT8 eofFlag);

UINT8 videoRecRemoveEndVoice(UINT32 idxSize, UINT32 rmvIdxNum);

UINT32 videoPbTimeLengthGet(void);

#endif /*__Video_H__*/
